<script>
	$(document).ready(function(){
		
		save_sap = function(){
			// save jika cell masih dlm keadaan edit
			$('#dg-sap').datagrid('endEdit', editIndex);
			
			$.ajax({
			  url: base_url+"matakuliah/save_sap/",
			  method: 'POST',
			  data: {
						kd_matakuliah : $('#kd_matakuliah').val(),
						kd_prodi : $('#kd_prodi').val(),
						deskripsi : $('#deskripsi').val(),
						kompetensi : $('#kompetensi').val(),
						daftar_pustaka : $('#daftar_pustaka').val(),
						kompetensi_dosen : $('#kompetensi_dosen').val(),
						data_sap : $('#dg-sap').datagrid('getData')
			  success : function(response, textStatus){
					},
				//alert(response);
				var response = eval('('+response+')');
				if(response.success){
					$.messager.show({
						title: 'Success',
						msg: 'Data Berhasil Disimpan'
					});
					$('#dialog-sap').dialog('close');
					//$('#dg').datagrid('reload');
				}else{
					$.messager.show({
						title: 'Error',
						msg: response.msg
					});
				}
			  }
			});
		}
		
		delete_sap = function(){
			$('#dg-sap').datagrid('endEdit', editIndex)
			var row = $('#dg-sap').datagrid('getSelected');
			if (row){
				if(row.kd_prodi == "" && row.kd_matakuliah == ""){ //delete langsung
					$('#dg-sap').datagrid('deleteRow', editIndex);
				}else{ // delete record dari database
					if(confirm("Tekan Ok jika akan menghapus data")){
						$.getJSON(base_url+"matakuliah/delete_sap/"+row.kd_prodi+'/'+row.kd_matakuliah+'/'+row.kd_materi, function( result ) {
							if (result.success){
								$.messager.show({
									title: 'Pesan',
									msg: 'Data berhasil hapus'
								});
								$('#dg-sap').datagrid('deleteRow', editIndex);
							} else {
								$.messager.show({
									title: 'Error',
									msg: result.msg
								});
							}
						});
					}
				}
			}
		}
		
		// editing cell
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});
		
		var editIndex = undefined;
		endEditing = function(){
			if (editIndex == undefined){return true}
			if ($('#dg-sap').datagrid('validateRow', editIndex)){
				$('#dg-sap').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		
		onClickCells = function(index, field){
			if (endEditing()){
				$('#dg-sap').datagrid('selectRow', index)
						.datagrid('editCell', {index:index,field:field});
				editIndex = index;
			}
		}
		
		$('#dg-sap').datagrid({
			data:<?=$data_sap?>
		});
		
		//tombol bawah
		$(function(){
			var pager = $('#dg-sap').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
				{
					iconCls:'icon-add',
					text:'Tambah',
					handler:function(){
						$('#dg-sap').datagrid('appendRow',{
							kd_prodi:'',
							kd_matakuliah:'',
							kd_materi:'',
							Materi:'',
							Waktu:'',
							Metode:''
						});
					}
				},
				{
					iconCls:'icon-remove',
					text:'Hapus',
					handler:function(){
						delete_sap();
						// $('#dg-sap').datagrid('endEdit', editIndex);
						// var xx = $('#dg-sap').datagrid('getData');
						// alert(JSON.stringify(xx));
					}
				}
				],
				layout:[],
				displayMsg:''
			});
		});
		
		
		
	});
</script>

<div style="margin:15px">
	<input type="hidden" name="kd_matakuliah" id="kd_matakuliah" value="<?=$kd_matakuliah?>">
	<input type="hidden" name="kd_prodi" id="kd_prodi" value="<?=$kd_prodi?>">

	<div class="fitem" >
		<label style="width:150px">Program Studi </label>:
		<b><?=$nama_prodi?></b>
	</div>
	<div class="fitem" >
		<label style="width:150px">Kode MK </label>:
		<b><?=$kd_matakuliah?></b>
	</div>
	<div class="fitem" >
		<label style="width:150px">Nama MK </label>:
		<b><?=$nama_matakuliah?></b>
	</div>
	
	<div class="fitem">
		<label style="width:150px;vertical-align:top;">Deskripsi </label>
		<textarea name="deskripsi" id="deskripsi" cols="50" rows="5"><?=$deskripsi?></textarea>
	</div>
	<div class="fitem">
		<label style="width:150px;vertical-align:top;">Kompetensi </label>
		<textarea name="kompetensi" id="kompetensi" cols="50" rows="5"><?=$kompetensi?></textarea>
	</div>
	<div class="fitem">
		<label style="width:150px;vertical-align:top;">Daftar Pustaka </label>
		<textarea name="daftar_pustaka" id="daftar_pustaka" cols="50" rows="5"><?=$daftar_pustaka?></textarea>
	</div>
	<div class="fitem">
		<label style="width:150px;vertical-align:top;">Kompetensi Dosen </label>
		<textarea name="kompetensi_dosen" id="kompetensi_dosen" cols="50" rows="5"><?=$kompetensi_dosen?></textarea>
	</div>
	<div class="fitem">
		<label style="width:150px;vertical-align:top;"> </label>
		<table id="dg-sap" style="width:575px;height:200px"
			data-options="	rownumbers:true,
							singleSelect:true,
							autoRowHeight:false,
							pagination:true,
							pageSize:50,
							pageList:[10,20,30,40,50,100,150,200],
							fit:false,
							onClickCell: onClickCells,
					    ">
		<thead>
			<tr>
				<th field="kd_prodi" hidden="true" sortable="false" width="80">x</th>
				<th field="kd_matakuliah" hidden="true" sortable="false" width="80">y</th>
				<th field="kd_materi" sortable="false" width="80" editor="text">KD Materi</th>
				<th field="Materi" sortable="false" width="200" editor="text">Nama Materi</th>
				<th field="Waktu" sortable="false" width="80" editor="text">Waktu</th>
				<th field="Metode" sortable="false" width="50" editor="text">Metode</th>
			</tr>
		</thead>
	</table>
	</div>
</div>

	

